import 'dart:async';
import 'dart:convert';
import 'package:flutter_trip/model/search_model.dart';
import 'package:http/http.dart' as http;

// 没用到
const SEARCH_URL =
    'https://m.ctrip.com/restapi/h5api/globalsearch/search?source=mobileweb&action=mobileweb&keyword=';

// 搜索大接口
class SearchDao {
  static Future<SearchModel> fetch(String url, String text) async {
    // 发起请求
    final respnse = await http.get(url);
    if (respnse.statusCode == 200) {
      Utf8Decoder utf8decoder = Utf8Decoder(); // fix 中午乱码
      var result = json.decode(utf8decoder.convert(respnse.bodyBytes)); // 解码
      // 只有当当前输入的内容和服务端返回的内容一致时才渲染
      SearchModel model = SearchModel.fromJson(result);
      model.keyword = text;
      return model;
    } else {
      throw Exception('Fail to load search_page.json');
    }
  }
}
